package com.admin.web;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.service.AdminSchoolService;
import com.common.model.AdminUser;
import com.common.model.BaseUser;
import com.common.model.Department;
import com.common.model.ProfBranch;
import com.common.model.Profession;

@Controller
@RequestMapping("/admin/profession")
public class AdminProfessionWeb {
	@Autowired
	AdminSchoolService service;
	/**
	 * 根据条件查询专业
	 * @param clgId 学院id
	 * @param depId 系id
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showProfessionList/{clg_id}/{dep_id}/{page}")
	public ModelAndView showProfession(
			@PathVariable("clg_id") int clgId,
			@PathVariable("dep_id")int depId,
			@PathVariable("page")int page){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/school/showProfession");
		mav.addObject("colleges", service.getAllCollege());
		mav.addObject("departments",service.getDepsOfCollege(clgId));
		mav.addObject("clgId", clgId);
		mav.addObject("depId", depId);
		int professionCount=service.getProfessionCountBy(clgId, depId);
		int totalPage=(int)Math.ceil(professionCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("profession",service.getProfessionBy(clgId, depId, page));
		mav.addObject("professionCount",professionCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showProfessionList");
		return mav;
	}
	@ResponseBody
	@RequestMapping("/getProfession/{pro_id}")
	public Profession getProfessionByProId(@PathVariable("pro_id") int proId){
		return service.getProfessionByProId(proId);
	}
	@ResponseBody
	@RequestMapping("/updateProfession")
	public int updateProfession(Profession pro,HttpSession session){
		if(pro.getId()==null||pro.getDepartId()==null||pro.getName()==null||pro.getName().trim().isEmpty()||pro.getNo()==null||pro.getNo().trim().isEmpty()){
			return -3;
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的专业是否为本学院的
			int orgCollegeId=service.getProfessionByProId(pro.getId()).getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -1;//操作之前不是本学院的
			}
			int newCollegeId=service.getDepartmentByDepId(pro.getDepartId()).getCollege().getId();
			if(admin.getCollegeId()!=newCollegeId){
				return -2;//操作之后不是本学院的
			}
		}
		Profession proCheck=service.getProfessionByProNo(pro.getNo());
		if(proCheck!=null&&proCheck.getId()!=pro.getId()){
			return -4;//重复no;
		}
		pro.setName(pro.getName().trim());
		pro.setNo(pro.getNo().trim());
		try {
			service.updateProfession(pro);
		} catch (Exception e) {
			return -5;//参数错误2
		}
		return 0;
	}
	/**
	 * 删除一个专业
	 * @param proId
	 * @return -2 权限不足,-1 专业下还有专业方向存在 0 成功
	 */
	@ResponseBody
	@RequestMapping("/deleteProfession/{pro_id}")
	public int deleteProfession(@PathVariable("pro_id")int proId,HttpSession session){
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的专业是否为本学院的
			int orgCollegeId=service.getProfessionByProId(proId).getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -2;//无权操作
			}
		}
		return service.deleteProfession(proId);
	}
	/**
	 * 增加一个专业
	 * @param pro
	 * @return -1 权限不足 -2,参数错误-3 专业编号重复 -4 参数错误2 0 成功
	 */
	@ResponseBody
	@RequestMapping("/insertProfession")
	public int insertProfession(Profession pro,HttpSession session){
		if(pro.getName()==null||pro.getName().trim().isEmpty()||pro.getNo()==null||pro.getNo().trim().isEmpty()||pro.getDepartId()==null){
			return -2;//参数错误
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查添加的专业是否为本学院的
			int newCollegeId=service.getProfessionByProId(pro.getId()).getDepartment().getCollege().getId();
			if(admin.getCollegeId()!=newCollegeId){
				return -1;//添加的专业不属于本学院
			}
		}
		pro.setName(pro.getName().trim());
		pro.setNo(pro.getNo().trim());
		int result=service.insertProfession(pro);
		if(result==-1){
			return -3;//已存在同样no的专业
		}
		if(result==-2){
			return -4;//参数错误2
		}
		return 0;
	}
}
